package com.hyts.apidoc.springmvc;

import com.google.common.collect.Maps;
import com.hyts.apidoc.swagger.SwaggerApiDoc;
import com.hyts.apidoc.swagger.bean.SwaggerBean;
import com.hyts.apidoc.swagger.handler.SwaggerAnnotationHandler;
import com.hyts.apidoc.swagger.yaml.YamlDataHandler;
import lombok.Builder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.apidoc.swagger.handler
 * author:Libo/Alex
 * create-date:2019-07-13 19:22
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于:
 */
@Builder
public class SpringmvcAnnotationHandler extends SwaggerAnnotationHandler {

    /**
     * swaggerApiDoc
     */
    private static SwaggerApiDoc swaggerApiDoc = new SwaggerApiDoc();

    /**
     * getPath服务
     * @param method
     * @return
     */
    @Override
    public String getPath(Method method) {
        RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
        if(StringUtils.isNoneBlank(requestMapping.value()[0])){
            return requestMapping.value()[0];
        }
        return requestMapping.path()[0];
    }

    /**
     * 创建apiDoc文档
     */
    public void createAopDoc(String packageName,String outFile){
        List<Class<?>> classList = swaggerApiDoc.init(packageName);
        YamlDataHandler yamlDataHandler =new YamlDataHandler();
        classList.parallelStream().forEach(param->{
            SwaggerBean swaggerBean = parseAnnotation(param);
            Map<String,Object> dataMap = Maps.newHashMap();
            dataMap.put("swagger",swaggerBean);
            yamlDataHandler.writer(dataMap,outFile);
        });
        swaggerApiDoc.createHtml(outFile,outFile);

    }

    public static void main(String[] args){
        swaggerApiDoc.createHtml("D:\\other_space\\coffice-toolbox\\coffice-toolbox-archive\\coffice-toolbox-swagger\\src\\main\\resources\\swagger\\swagger.yaml","D:\\other_space\\coffice-toolbox\\coffice-toolbox-archive\\coffice-toolbox-swagger\\src\\main\\resources\\swagger\\template-test2.html");
        //SpringmvcAnnotationHandler.builder().build().createAopDoc("com.hyts.apidoc","/Users/alex/Documents/我的代码/coffice-toolbox/coffice-toolbox-assemble/coffice-toolbox-apidoc/src/main/resources/swagger/template-test2.yaml");
    }
}
